# -*- coding:utf-8 -*-

import models, service
from .ServiceException import ServiceException

companyTypeDisplayTable = {
    models.Company.TYPE_BRANCH : u'分公司',
    models.Company.TYPE_ASSESS : u'公估公司',
    models.Company.TYPE_INSURANCE : u'保险公司'
}

def getCompany(companyId):
    if not companyId:
        raise ServiceException(service.COMPANY_ID_NULL, '公司id为空')
    company = models.Company.getById(companyId)
    if not company:
        raise ServiceException(service.COMPANY_NOT_EXSIT, '公司不存在')
    return company

def getBranchCompany(companyId):
    if not companyId:
        raise ServiceException(service.COMPANY_ID_NULL, '公司id为空')
    company = models.Company.getById(companyId)
    if not company:
        raise ServiceException(service.COMPANY_NOT_EXSIT, '公司不存在')
    elif models.Company.TYPE_BRANCH != company.type:
        raise ServiceException(service.NOT_BRANCH_COMPANY, '非分公司')
    return company

def getAllCompanys():
    companys = models.Company.getAllCompanys()
    for company in companys:
        company.type = companyTypeDisplayTable[company.type]
    return companys